﻿using B.S.Traceable.Domain.Entitiy;
using B.S.Traceable.ErrorCode;
using B.S.Traceable.Infrasturcture.Interfaces;
using B.S.Traceable.Write.Api.Application.Command.Traceable;
using MediatR;

namespace B.S.Traceable.Write.Api.Application.CommandHandler.Traceable
{
    public class DelTraceableHandler : IRequestHandler<DelTraceableCommand, ApiResult<object>>
    {
        /// <summary>
        /// www1
        /// </summary>
        private readonly IBaseRepository<TraceableModel> traRepo;

        public DelTraceableHandler(IBaseRepository<TraceableModel> traRepo)
        {
            this.traRepo = traRepo;
        }

        public Task<ApiResult<object>> Handle(DelTraceableCommand request, CancellationToken cancellationToken)
        {
            ApiResult<object> result = new ApiResult<object>();
            
            var Del=traRepo.GetAll().FirstOrDefault(x=>x.TraceableId==request.TraceableId);
            Del.IsDeleted= true;

            var s=traRepo.Update(Del).Result;
            if (s > 0)
            {
                result.Code = ApiEnums.Success;
                result.Msg = "删除成功";
                result.Data = s;
            }
            else
            {
                result.Code = ApiEnums.Failed;
                result.Msg = "删除失败";
                result.Data = s;
            }
            return Task.FromResult(result);
        }
    }
}
